package com.pattern.agency;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class UserProxy {
    private IUserService iUserService;

    public UserProxy(IUserService iUserService) {
        this.iUserService = iUserService;
    }

    public Object createProxy() {

        return Proxy.newProxyInstance(iUserService.getClass().getClassLoader(), iUserService.getClass().getInterfaces(), new UserProxyHandler());
    }

    private class UserProxyHandler implements InvocationHandler {
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println("start---------");
            Object invoke = method.invoke(iUserService, args);
            System.out.println("end----------");
            return invoke;
        }
    }

}
